#include "captuerScreenCallback.h"


void Osgis::CaptureScreenCallback::operator()( osg::RenderInfo& renderInfo ) const
{
	bool capturing = false;
	if(_image.valid() && _image->getUserValue("Capture", capturing))
	{
		osg::GraphicsContext* context = renderInfo.getState()->getGraphicsContext();
		if(capturing && context->getTraits())
		{
			int width = context->getTraits()->width;
			int height = context->getTraits()->height;
			GLenum pixelFormat = (context->getTraits()->alpha? GL_RGBA:GL_RGB);
			_image->readPixels(0, 0, width, height, pixelFormat, GL_UNSIGNED_BYTE);
			std::stringstream ss;
			ss<<"IMG-"<<(++_fileIndex)<<".bmp";
			if(osgDB::writeImageFile(*_image, ss.str()) && _text.valid())
				_text->setText(std::string("Saved image to ") + ss.str());	
		}
		_image->setUserValue("Capture", false);
	}
}
